<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Dao;

use App\Model\SendCodeLog;
use Hyperf\Di\Annotation\Inject;

class SendCodeLogDao
{
    /**
     * @Inject
     * @var SendCodeLog
     */
    protected $sendCodeLogModel;

    /**
     * 新增一个验证码发送记录.
     * @param $account
     * @param $code
     */
    public function addLog($account, $code): int
    {
        return $this->sendCodeLogModel->addLog($account, $code);
    }

    /**
     * 获取验证码发送记录.
     * @param $account
     * @param $code
     */
    public function getLog($account, $code): array
    {
        $info = \App\Model\SendCodeLog::where('account', $account)
            ->where('code', $code)->first();
        if (! is_null($info)) {
            return $info->toArray();
        }
        return [];
    }

    /**
     * 作废验证码
     * @param $account
     * @param $code
     */
    public function invalidCode($account, $code): void
    {
        SendCodeLog::query()->where('account', $account)
            ->where('code', $code)->delete();
    }
}
